#!/usr/bin/env python
# -*- coding:utf-8 -*-

from base import (dingding_push, read4csv, get_rates4pos, generate_data, write_2csv, initialize_mt5,
                  conversion_tradetime, judge_price4value, judge_Kline_color)
from datetime import datetime
import MetaTrader5 as mt5
import pandas as pd
import yaml, time
import requests
from datetime import datetime

with open('config.yaml', 'r', encoding='utf-8') as f:
    config = yaml.safe_load(f)
    run_mod = config['run_mod']
    begin = config['begin']
    symbol = config['symbols'][0]
    min_level = config['level']['min_level']
    mid_level = config['level']['mid_level']
    large_level = config['level']['lar_level']
    level_str = config['level']['title']
i = 0
begin_time = datetime.strptime(begin, "%Y.%m.%d %H:%M:%S")
lar_df = read4csv(symbol, large_level)
mid_df = read4csv(symbol, mid_level)
min_df = read4csv(symbol, min_level)
test_time = begin_time + pd.Timedelta(minutes=mid_level * i)

while True:
    i += 1
    if test_time.weekday() >= 5:
        continue   # 跳过周末
    min_test_time = conversion_tradetime(test_time, min_level)
    mid_test_time = conversion_tradetime(test_time, mid_level)
    lar_test_time = conversion_tradetime(test_time, large_level)

    min_df['TradeTime'] = pd.to_datetime(min_df['TradeTime'])
    mid_df['TradeTime'] = pd.to_datetime(mid_df['TradeTime'])
    lar_df['TradeTime'] = pd.to_datetime(lar_df['TradeTime'])

    min_target_df = min_df[min_df['TradeTime'] <= min_test_time]
    mid_target_df = mid_df[mid_df['TradeTime'] <= mid_test_time]
    lar_target_df = lar_df[lar_df['TradeTime'] <= lar_test_time]

    price = mid_target_df['close'].values[-1]
    now = str(test_time)
    # print(mid_df)


    time.sleep(15)

# # 示例数据：假设我们有一些价格数据（开盘价、最高价、最低价、收盘价）
# np.random.seed(42)
# high = np.cumsum(np.random.randn(100)) + 100  # 随机生成最高价
# low = np.cumsum(np.random.randn(100)) + 90    # 随机生成最低价
# close = np.cumsum(np.random.randn(100)) + 95  # 随机生成收盘价
#
# # 计算随机指标
# # 参数1：最高价
# # 参数2：最低价
# # 参数3：收盘价
# # 参数4：%K 的时间周期（例如14天）
# # 参数5：%D 的平滑周期（例如3天）
# # 参数6：%D 的平滑方法（0：简单移动平均，1：指数移动平均）
# slowk, slowd = talib.STOCH(high, low, close, fastk_period=14, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
#
# # 将结果转换为DataFrame以便更好地查看
# result = pd.DataFrame({
#     'High': high,
#     'Low': low,
#     'Close': close,
#     '%K': slowk,
#     '%D': slowd
# })
